/*
 * @lc app=leetcode.cn id=2544 lang=cpp
 *
 * [2544] 交替数字和
 */

// @lc code=start
#include<bits/stdc++.h>
using namespace std;
class Solution
{
public:
    int alternateDigitSum(int n)
    {
        int ans = 0;
        int sign = 1;

        if(int(log10(n)) & 1)
        {
            sign = -1;
        }
        else
        {
            sign = 1;
        }

        while (n)
        {
            int tmp = n % 10;
            ans += tmp * sign;
            sign = -sign;
            n /= 10;
        }

        return ans;
    }
};
// @lc code=end
